"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const fs_promise_1 = __importDefault(require("fs-promise"));
const colors_console_1 = __importDefault(require("colors-console"));
const sleep_1 = __importDefault(require("../lib/sleep"));
const RSATool_1 = __importDefault(require("../lib/RSATool"));
let configInit = false;
const defaultBaseConfig = {
    'version': 'v2.9',
    'clientCORS': true,
    'clientServerPort': 3000,
    'manageServerPort': 3001,
    'wsServerPort': 3001,
    'clientBlackList': [],
    'manageWhiteList': [
        '::ffff:127.0.0.1'
    ],
    'wwwPath': 'www',
    'MapBox': {
        'access_token': 'pk.eyJ1Ijoic3ZjLW9rdGEtbWFwYm94LXN0YWZmLWFjY2VzcyIsImEiOiJjbG5sMnExa3kxNTJtMmtsODJld24yNGJlIn0.RQ4CHchAYPJQZSiUJ0O3VQ',
        'header': {
            'Referer': 'https://geojson.io/'
        }
    },
    'TianDiTu': {
        'tk': ''
    },
    'CacheSave': {
        'CacheSaveFile': true,
        'CacheSaveDB': false
    },
    'userList': [
        { 'userName': 'user_1', 'nickname': '访客', 'password': '123456', 'role': 'client' },
        { 'userName': 'admin', 'nickname': '管理员', 'password': '123456', 'role': 'manage' }
    ],
    'publicDer': '',
    'privateDer': ''
};
const defaultServiceConfig = {};
const configTool = {
    appBasePath: '',
    wwwPath: '',
    baseConfig: defaultBaseConfig,
    serviceConfig: defaultServiceConfig,
    async editConfig(key, value) {
        await this.init();
        let result = false;
        if (typeof this.baseConfig[key] === typeof value) {
            this.baseConfig[key] = value;
            await fs_promise_1.default.writeJSON(path_1.default.join(this.appBasePath, '/conf/baseConfig.json'), this.baseConfig);
            result = true;
        }
        return result;
    },
    async init() {
        return new Promise(async (resolve, reject) => {
            if (!configInit) {
                configInit = true;
                let isDev = !__dirname.includes('snapshot');
                if (isDev) {
                    this.appBasePath = path_1.default.join(__dirname, '../../');
                }
                else {
                    this.appBasePath = path_1.default.dirname(process.execPath);
                }
                await fs_promise_1.default.ensureDir(path_1.default.join(this.appBasePath, '/conf'));
                await fs_promise_1.default.ensureDir(path_1.default.join(this.appBasePath, '/temp/fileOut'));
                await fs_promise_1.default.ensureDir(path_1.default.join(this.appBasePath, '/temp/fileDB'));
                await fs_promise_1.default.ensureDir(path_1.default.join(this.appBasePath, '/temp/logs/clientServer'));
                await fs_promise_1.default.ensureDir(path_1.default.join(this.appBasePath, '/temp/logs/manageServerPort'));
                await fs_promise_1.default.ensureDir(path_1.default.join(this.appBasePath, '/www'));
                let configFile = path_1.default.join(this.appBasePath, '/conf/baseConfig.json');
                let serviceFile = path_1.default.join(this.appBasePath, '/conf/serviceConfig.json');
                await fs_promise_1.default.readJson(configFile).then((d) => {
                    if (d.version === defaultBaseConfig.version) {
                        this.baseConfig = d;
                    }
                    else {
                        console.log((0, colors_console_1.default)('red', '----|||配置文件版本过低，未生效！！请重置配置文件|||----'));
                        throw new Error('配置文件版本不匹配');
                    }
                }).catch(() => {
                    this.baseConfig = defaultBaseConfig;
                    const { privateDer, publicDer } = RSATool_1.default.createKey(2048);
                    this.baseConfig.privateDer = privateDer;
                    this.baseConfig.publicDer = publicDer;
                    fs_promise_1.default.writeJSON(configFile, this.baseConfig);
                });
                await fs_promise_1.default.readJson(serviceFile).then((d) => {
                    this.serviceConfig = d;
                }).catch(() => {
                    fs_promise_1.default.writeJSON(serviceFile, this.serviceConfig);
                });
                if (path_1.default.isAbsolute(this.baseConfig.wwwPath)) {
                    this.wwwPath = path_1.default.join(this.baseConfig.wwwPath);
                }
                else {
                    this.wwwPath = path_1.default.join(this.appBasePath, this.baseConfig.wwwPath);
                }
            }
            while (!configInit) {
                await (0, sleep_1.default)(50);
            }
            resolve(true);
        });
    },
    preservableONDB() {
        return this.baseConfig.CacheSave.CacheSaveDB;
    },
    preservableONFile() {
        return this.baseConfig.CacheSave.CacheSaveFile;
    }
};
exports.default = configTool;
